package com.gate.ws.spring.process.upmsg;

import com.gate.ws.spring.request.RpcConfig;
import com.gate.ws.spring.constants.ChannelConstants;
import com.gate.ws.spring.service.MqSender;
import com.gate.ws.spring.utils.JsonUtils;
import com.nami.basic.bo.servicetrans.up.UpMsgWrap;
import com.nami.basic.constants.UpTopicConst;
import com.nami.basic.packet.Packet;
import io.netty.channel.Channel;
import io.netty.util.AttributeKey;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;

public class QosProcess implements PacketProcessor {

    private static final Logger log = LoggerFactory.getLogger(QosProcess.class);

    private RpcConfig rpcConfig;

    private MqSender mqSender;

    public QosProcess(MqSender mqSender, RpcConfig rpcConfig) {
        this.mqSender = mqSender;
        this.rpcConfig = rpcConfig;
    }

    @Override
    public void process(Channel channel, Packet packet) throws Exception {
        try {
            mqSender.send(UpTopicConst.QOS_TOPIC, JsonUtils.toJsonString(newC2CMSg(channel, packet)));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @NotNull
    private UpMsgWrap newC2CMSg(Channel channel, Packet packet) {
        UpMsgWrap wrap = new UpMsgWrap();
        wrap.setDeviceType((Integer) channel.attr(AttributeKey.valueOf(ChannelConstants.DEVICE_TYPE)).get());
        InetSocketAddress socketAddress = (InetSocketAddress) channel.remoteAddress();
        wrap.setUserIp(socketAddress.getAddress().getHostAddress());
        wrap.setPacket(packet);
//        log.info("[upmsg] qos. body:{}", ToStringBuilder.reflectionToString(wrap, ToStringStyle.SHORT_PREFIX_STYLE));
        return wrap;
    }


}
